package net.realsee.controller.wcs.data;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import net.realsee.common.annotation.Log;
import net.realsee.common.annotation.RepeatSubmit;
import net.realsee.common.enums.BusinessType;
import net.realsee.common.utils.poi.ExcelUtil;
import net.realsee.domain.wcs.data.bo.WcsUnitBo;
import net.realsee.domain.wcs.data.vo.WcsUnitVo;
import net.realsee.framework.core.controller.BaseController;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.domain.R;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.framework.core.validate.AddGroup;
import net.realsee.framework.core.validate.EditGroup;
import net.realsee.service.wcs.data.IWcsUnitService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.Arrays;
import java.util.List;

/**
 * UNIT
 *
 * @author realsee
 * @date 2023-08-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/wcs/unit")
public class WcsUnitController extends BaseController {

    private final IWcsUnitService iWcsUnitService;

    /**
     * 查询UNIT列表
     */
    @SaCheckPermission("wcs:unit:list")
    @GetMapping("/list")
    public TableDataInfo<WcsUnitVo> list(WcsUnitBo bo, PageQuery pageQuery) {
        return iWcsUnitService.selectVoPage(bo, pageQuery);
    }

    /**
     * 导出UNIT列表
     */
    @SaCheckPermission("wcs:unit:export")
    @Log(title = "UNIT", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(WcsUnitBo bo, HttpServletResponse response) {
        List<WcsUnitVo> list = iWcsUnitService.selectVoList(bo);
        ExcelUtil.exportExcel(list, "UNIT", WcsUnitVo.class, response);
    }

    /**
     * 获取UNIT详细信息
     *
     * @param unitId 主键
     */
    @SaCheckPermission("wcs:unit:query")
    @GetMapping("/{unitId}")
    public R<WcsUnitVo> getInfo(@NotNull(message = "主键不能为空")
                                @PathVariable Long unitId) {
        return R.ok(iWcsUnitService.selectVoById(unitId));
    }

    /**
     * 新增UNIT
     */
    @SaCheckPermission("wcs:unit:add")
    @Log(title = "UNIT", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WcsUnitBo bo) {
        return toAjax(iWcsUnitService.insertByBo(bo));
    }

    /**
     * 修改UNIT
     */
    @SaCheckPermission("wcs:unit:edit")
    @Log(title = "UNIT", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WcsUnitBo bo) {
        return toAjax(iWcsUnitService.updateByBo(bo));
    }

    /**
     * 删除UNIT
     *
     * @param unitIds 主键串
     */
    @SaCheckPermission("wcs:unit:remove")
    @Log(title = "UNIT", businessType = BusinessType.DELETE)
    @DeleteMapping("/{unitIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空") @PathVariable Long[] unitIds) {
        return toAjax(iWcsUnitService.deleteWithValidByIds(Arrays.asList(unitIds)));
    }
}
